package com.csjung.games.gamenori.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.i18n.client.HasDirection;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

public class WindowDialogBox extends DialogBox {

  /**
   * Image resources.
   */
  public static interface Resources extends ClientBundle {

    @Source("window-button-close.png")
    ImageResource windowButtonClose();

    @Source("window-button-maximize.png")
    ImageResource windowButtonMaximize();

    @Source("window-button-minimize.png")
    ImageResource windowButtonMinimize();
  }
  
  /**
   * Represents how the window panel appears on the page.
   */
  public enum WindowState {
    MAXIMIZED, MINIMIZED
  }

  private final Resources RESOURCES = GWT.create(Resources.class);
  private final Image CLOSE_ICON = makeImage(RESOURCES.windowButtonClose());
  private final Image MINIMIZE_ICON = makeImage(RESOURCES.windowButtonMinimize());
  private final Image MAXIMIZE_ICON = makeImage(RESOURCES.windowButtonMaximize());
  private final FlexTable captionLayoutTable = new FlexTable();
  private final ImageButton closeBtn = new ImageButton(new Image(CLOSE_ICON.getUrl(),
      CLOSE_ICON.getOriginLeft(), CLOSE_ICON.getOriginTop(), CLOSE_ICON.getWidth(),
      CLOSE_ICON.getHeight()));
  private final ImageButton minimizeBtn = new ImageButton(new Image(MINIMIZE_ICON.getUrl(),
      MINIMIZE_ICON.getOriginLeft(), MINIMIZE_ICON.getOriginTop(), MINIMIZE_ICON.getWidth(),
      MINIMIZE_ICON.getHeight()));
  
  private final ImageButton maximizeBtn = new ImageButton(new Image(MAXIMIZE_ICON.getUrl(),
      MAXIMIZE_ICON.getOriginLeft(), MAXIMIZE_ICON.getOriginTop(), MAXIMIZE_ICON.getWidth(),
      MAXIMIZE_ICON.getHeight()));

  private WindowState windowState = WindowState.MAXIMIZED;
  
  private Widget nestWidget;
  
  @Override
  public void setText(String text) {
    captionLayoutTable.setWidth("100%");
    captionLayoutTable.setText(0, 0, text);
    captionLayoutTable.setWidget(0, 1, minimizeBtn);
    captionLayoutTable.setWidget(0, 2, closeBtn);
    captionLayoutTable.getCellFormatter().setHorizontalAlignment(0, 1,
        HasHorizontalAlignment.HorizontalAlignmentConstant.endOf(HasDirection.Direction.LTR));
    captionLayoutTable.getCellFormatter().setHorizontalAlignment(0, 2,
        HasHorizontalAlignment.HorizontalAlignmentConstant.endOf(HasDirection.Direction.LTR));
    HTML caption = (HTML) getCaption();
    caption.getElement().appendChild(captionLayoutTable.getElement());

    getCaption().addMouseDownHandler(new MouseDownHandler() {
      @Override
      public void onMouseDown(MouseDownEvent event) {
        EventTarget target = event.getNativeEvent().getEventTarget();
        Element targetElement = (Element) target.cast();
        if (targetElement == 
            ((ImageButton)captionLayoutTable.getWidget(0,2)).getImage().getElement()) {
          hide();
        } else if (targetElement == 
            ((ImageButton)captionLayoutTable.getWidget(0,1)).getImage().getElement()) {
          if (windowState == WindowState.MAXIMIZED) {
            minimize();
          } else {
            maximize();
          }
        }
      }
    });
  }
  
  private Image makeImage(ImageResource resource) {
    AbstractImagePrototype proto = AbstractImagePrototype.create(resource);
    return proto.createImage();
  }

  private void minimize() {
    windowState = WindowState.MINIMIZED;
    captionLayoutTable.setWidget(0, 1, maximizeBtn);
    if (getWidget() != null) {
      nestWidget = getWidget(); 
      clear();
    }
  }
  
  private void maximize() {
    windowState = WindowState.MAXIMIZED;
    captionLayoutTable.setWidget(0, 1, minimizeBtn);
    if (getWidget() == null) {
      add(nestWidget);
    }
  }

}
